package com.easy.web.controller.ord;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.core.R;
import com.easy.merchants.pojo.HouseInformation;
import com.easy.merchants.service.MerchantsService;
import com.easy.user.pojo.Order;
import com.easy.user.pojo.dto.OrderDto;
import com.easy.user.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单控制器
 *
 * @author RuoYu
 * @date 2023/02/03
 */
@RestController
@RequestMapping("order")
@Api(tags = "订单控制器")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private MerchantsService merchantsService;

    @GetMapping("page")
    @ApiOperation(value = "获取订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", required = true),
            @ApiImplicitParam(name = "name", value = "搜索参数", required = false),
    })
    public R<Page<OrderDto>> page(int page, int pageSize, String name) {
        Page<Order> pageInfo = orderService.page(page, pageSize, name);
        Page<OrderDto> orderDtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo,orderDtoPage,"records");

        List<Order> orderList = pageInfo.getRecords();
        List<OrderDto> orderDtoList = orderList.stream().map((t) -> {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(t, orderDto);
            orderDto.setId(null);
            Long rid = t.getRid();//房屋信息id
            LambdaQueryWrapper<HouseInformation> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(HouseInformation::getId, rid);
            HouseInformation one = merchantsService.getOne(wrapper);
            String img = one.getImg();
            orderDto.setHouseImg(img);
            return orderDto;
        }).collect(Collectors.toList());

        orderDtoPage.setRecords(orderDtoList);
        return R.success(orderDtoPage);
    }

    /**
     * 交易订单凭证
     *
     * @param rid 房屋对象id
     * @return {@link R}
     */
    @PostMapping
    @ApiOperation(value = "交易订单凭证")
    @ApiImplicitParam(name = "rid", value = "房屋信息主键", required = true)
    public R certificate(Long rid) {
        orderService.certificate(rid);
        return R.success();
    }

    /**
     * 删除订单凭证
     *
     * @param id 主键
     * @return {@link R}
     */
    @ApiOperation(value = "删除订单凭证")
    @ApiImplicitParam(name = "id", value = "房屋信息主键", required = true)
    @DeleteMapping
    public R deleteCertificate(Long id) {
        orderService.deleteCertificate(id);
        return R.success();
    }
}
